package com.acheng.achengutils.widgets;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.acheng.achengutils.R;


/**
 * Created by acheng on 2016/3/14.
 */
public class MyWebViewWithSwipRefreshLayout extends LinearLayout {
    private SwipeRefreshLayout mSwipe_refresh;
    private WebView webView;
    private WebSettings settings;

    boolean allowDragTop = true; // 如果是true，则允许拖动至底部的下一页
    float downY = 0;
    boolean needConsumeTouch = true; // 是否需要承包touch事件，needConsumeTouch一旦被定性，则不会更改
    private boolean canRefresh;

    public MyWebViewWithSwipRefreshLayout(Context context) {
        this(context, null);
    }

    public MyWebViewWithSwipRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        View.inflate(getContext(), R.layout.webview_with_swiprefresh_layout, this);
        mSwipe_refresh = (SwipeRefreshLayout) findViewById(R.id.mSwipe_refresh);
        webView = (WebView) findViewById(R.id.webView);
        webView.clearCache(true);
        webView.clearHistory();
        webView.clearFormData();
        webView.clearMatches();
        settings = webView.getSettings();
        settings.setUserAgentString(String.format("%s |%s", settings.getUserAgentString(), "SuperShipApp"));
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setSupportZoom(true);
        settings.setJavaScriptEnabled(true);// 表示支持js
        settings.setBuiltInZoomControls(true);// 显示放大缩小按钮
        settings.setUseWideViewPort(true);// 支持双击缩放
        mSwipe_refresh.setEnabled(false);

        //屏蔽长按事件
        webView.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        mSwipe_refresh.setColorSchemeColors(ContextCompat.getColor(getContext(), R.color.app_style));
        mSwipe_refresh.setDistanceToTriggerSync(200);
        mSwipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });
    }

    public WebView getWebView() {
        if (webView == null) {
            initView();
        }
        return webView;
    }

    public SwipeRefreshLayout getmSwipe_refresh() {
        if (mSwipe_refresh == null) {
            initView();
        }
        return mSwipe_refresh;

    }

//    private void canRefresh(boolean canRefresh) {
//        this.canRefresh = canRefresh;
//    }


    private void loadUrl(String url) {
        webView.loadUrl(url);
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            downY = ev.getRawY();
            needConsumeTouch = true; // 默认情况下，listView内部的滚动优先，默认情况下由该listView去消费touch事件
            allowDragTop = isAtTop();
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            if (!needConsumeTouch) {
                if (!isAtTop()) {    //!isAtTop() || !canRefresh
                    mSwipe_refresh.setEnabled(false);
                } else {
                    mSwipe_refresh.setEnabled(true);
                }
            } else if (allowDragTop) {
//                if (!canRefresh){
                mSwipe_refresh.setEnabled(false);
//                }
                // needConsumeTouch尚未被定性，此处给其定性
                // 允许拖动到底部的下一页，而且又向上拖动了，就将touch事件交给父view
                if (ev.getRawY() - downY > 2) {
                    // flag设置，由父类去消费
                    needConsumeTouch = false;
                    getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            }
        }

        return super.dispatchTouchEvent(ev);
    }

    /**
     * 判断WebView是否在顶部
     *
     * @return 是否在顶部
     */
    private boolean isAtTop() {
        return webView.getScrollY() == 0;
    }
}
